//
//  LZMyCompanyInfoTableViewCell.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/17.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyCompanyInfoTableViewCell.h"
#import "Masonry.h"
#import "LZMerchantInfoModel.h"
#import "UIImageView+WebCache.h"

@interface LZMyCompanyInfoTableViewCell()

@property (nonatomic, strong) UILabel * lzTitleLabel;
@property (nonatomic, strong) UILabel * lzSubLabel;
@property (nonatomic, strong) UIImageView * lzImageView;
@property (nonatomic, strong) UIImageView * lzMarkImage;
@property (nonatomic, strong) UIView * line;

@end

@implementation LZMyCompanyInfoTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andType:(LZ_MY_COMPANYINFO_CELLTYPE)type
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.type = type;
        
        [self configView];
        [self addAutoLayoutToCell];
    }
    return self;
}

- (void)configView {
    
    if (self.type == LZ_MY_COMPANYINFO_CELLTYPE_TITLE) {
        [self.contentView addSubview:self.lzTitleLabel];
        [self.contentView addSubview:self.lzSubLabel];
    }
    else if (self.type == LZ_MY_COMPANYINFO_CELLTYPE_IMAGE) {
        [self.contentView addSubview:self.lzTitleLabel];
        [self.contentView addSubview:self.lzImageView];
    }
    else {
        [self.contentView addSubview:self.lzTitleLabel];
        [self.contentView addSubview:self.lzSubLabel];
        [self.contentView addSubview:self.lzImageView];
    }
    
    [self.contentView addSubview:self.line];
}

- (void)addAutoLayoutToCell {
    
    WS(weakSelf);
    if (self.type == LZ_MY_COMPANYINFO_CELLTYPE_TITLE) {
        [self.lzTitleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.leading.mas_equalTo(10);
            make.bottom.mas_equalTo(0);
        }];
        
        [self.lzSubLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.leading.mas_equalTo(weakSelf.lzTitleLabel.mas_trailing).with.offset(20);
            make.bottom.mas_equalTo(0);
        }];
    }
    else if (self.type == LZ_MY_COMPANYINFO_CELLTYPE_IMAGE) {
        [self.lzTitleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.leading.mas_equalTo(10);
            make.bottom.mas_equalTo(0);
        }];
        
        [self.lzImageView makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(weakSelf.lzTitleLabel.mas_trailing).with.offset(20);
            make.centerY.mas_equalTo(weakSelf.centerY);
            make.size.mas_equalTo(CGSizeMake(60, 60));
        }];
    }
    else {
        [self.lzTitleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(15);
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(-10);
            make.height.mas_equalTo(20);
        }];
        
        [self.lzImageView makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(weakSelf.lzTitleLabel.mas_bottom).with.offset(16);
            make.leading.mas_equalTo(10);
            make.size.mas_equalTo(CGSizeMake(18, 18));
        }];
        
        [self.lzSubLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(weakSelf.lzTitleLabel.mas_bottom).with.offset(15);
            make.leading.mas_equalTo(weakSelf.lzImageView.mas_trailing).with.offset(5);
            make.trailing.mas_equalTo(-10);
        }];
    }
    
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(0.6);
    }];
}

- (UILabel *)lzTitleLabel {

    if (_lzTitleLabel == nil) {
        _lzTitleLabel = [[UILabel alloc] init];
        _lzTitleLabel.textColor = UIColorFromRGB(0x333333, 1);
        _lzTitleLabel.font = [UIFont systemFontOfSize:15];
    }
    
    return _lzTitleLabel;
}

- (UILabel *)lzSubLabel {

    if (_lzSubLabel == nil) {
        _lzSubLabel = [[UILabel alloc] init];
        _lzSubLabel.textColor = UIColorFromRGB(0x333333, 1);
        _lzSubLabel.font = [UIFont systemFontOfSize:15];
    }
    
    return _lzSubLabel;
}

- (UIImageView *)lzImageView {

    if (_lzImageView == nil) {
        _lzImageView = [[UIImageView alloc] init];
        _lzImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)];
        [_lzImageView addGestureRecognizer:tapGR];
    }
    
    return _lzImageView;
}

- (UIView *)line {

    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

- (void)setMerchantInfoModel:(LZMerchantInfoModel *)merchantInfoModel {
    
    _merchantInfoModel = merchantInfoModel;
    
    switch (self.tag) {
        case 0: {
            self.lzTitleLabel.text = LZLocalizedString(@"company_name", nil);
            self.lzSubLabel.text = merchantInfoModel.companyName;
        }
            break;
        case 1: {
            self.lzTitleLabel.text = LZLocalizedString(@"business_lic", nil);
            [self.lzImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", APIBaseImageUrl, merchantInfoModel.licencePhoto]]
                                  placeholderImage:[UIImage imageNamed:@"lz_other_placehoiderImage"]];
        }
            break;
        case 2: {
            self.lzTitleLabel.text = LZLocalizedString(@"payment_account", nil);
            self.lzImageView.image = [UIImage imageNamed:@"lz_companyselect_image"];
            
            if ([merchantInfoModel.paymentMethod integerValue] == 1) {
                self.lzSubLabel.text = [NSString stringWithFormat:@"paypal:%@", merchantInfoModel.paypalNumber];
            } else {
                self.lzSubLabel.text = [NSString stringWithFormat:@"%@:%@", merchantInfoModel.bankName, merchantInfoModel.bankNumber];
            }
        }
            break;
            
        default:
            break;
    }
}

- (void)tapGRAction:(UITapGestureRecognizer *)sender {
    if (self.didTapImageBlock) {
        self.didTapImageBlock(sender);
    }
}

@end
